<script setup lang="ts">
import { getCurrentInstance, ref, onBeforeMount } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const userInfo = proxy.$Store.get('userInfo') || {}
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = {
  isOld: Number(logUser.is_news) === 2,
  isPopularize: Number(logUser.is_popularize) === 1,
  marryRegId: undefined,
  // ----
  forAdm: String(proxy.$Phone) === String(logUser.telphone)
}
// ----
const items = ref([{
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '商务信息',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'InfoAdm') }
  }, {
    show: true,
    isArrow: true,
    name: '在线客服',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'WechatAdm') }
  }, {
    show: !user.forAdm,
    isBtn: true,
    isOpen: user.isPopularize,
    name: '推广展示',
    callback: (item) => {
      const obj = event.target
      if (String(obj.tagName) === 'LABEL') {
        let forTitle = '欢迎加入【万域商友会】'
        let forCon = '你还没有提交商务信息'
        let forBtn = '立即提交'
        let forCallback = () => {
          layerBox.value.close()
          // ----
          proxy.$Myfun.openPage(proxy, 'push', 'InfoAdm')
        }
        if (user.isOld) {
          forTitle = '商务信息展示在万域商友栏目中'
          forCon = '是否确定展示'
          forBtn = '确定展示'
          let forPopularize = 1
          if (item.isOpen) {
            forCon = '是否暂停展示'
            forBtn = '确定暂停'
            forPopularize = 0
          }
          forCallback = () => {
            layerBox.value.close()
            // ----
            setPublicInfo({
              type: 'patch',
              url: 'dealer/update?id=' + logUser.id,
              data: {
                is_popularize: forPopularize,
                // ----
                is_stoped: Number(forPopularize) === 0 ? undefined : 1,
                // ----
                config_id: userInfo.config_id,
                openid: userInfo.openid
              }
            }, (res) => { item.isOpen = !item.isOpen })
          }
        }
        const options = {
          title: forTitle,
          content: forCon,
          button: [{
            text: proxy.$Global.layer.btnCancel
          }, {
            text: forBtn,
            class: 'xw-color-24',
            callback: forCallback
          }]
        }
        proxy.$Myfun.isIndepth(layerBox.value, 'open', options)
      }
    }
  }]
}, {
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '客户订单',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'ForOrderOther') }
  }, {
    show: true,
    isArrow: true,
    name: '我的订购',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'ForOrderSelf') }
  }]
}, {
  show: false,
  child: [{
    show: true,
    isArrow: true,
    name: '婚介交友',
    callback: () => {
      const options = {
        title: '万域商友会婚介交友',
        content: '<div style="line-height: 3rem; font-size: 1.7rem;">无费用，精准介绍为你喜结良缘<br/>有温度，贴心服务确保门当户对</div>',
        button: [{
          text: proxy.$Global.layer.btnCancel
        }, {
          text: '我要报名',
          class: 'xw-color-24',
          callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'MarryReg', { id: user.marryRegId }) }
        }]
      }
      proxy.$Myfun.isIndepth(layerBox.value, 'open', options)
    }
  }, {
    show: true,
    isArrow: true,
    name: '我们在哪',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'OfficeShare') }
  }]
}, {
  show: false,
  child: [{
    show: true,
    isArrow: true,
    name: '招商招聘',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CapitalShare') }
  }]
}, {
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '修改手机',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'TelphoneAdm') }
  }]
}, {
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '交费记录',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'ForPayRecord') }
  }]
}, {
  show: user.forAdm,
  child: [{
    show: true,
    isArrow: true,
    name: '系统管理',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'ForSet') }
  }]
}, {
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '退出系统',
    callback: () => {
      const options = {
        title: proxy.$Global.layer.titleOne,
        content: '是否确定退出',
        button: [{
          text: proxy.$Global.layer.btnCancel
        }, {
          text: '确定退出',
          class: 'xw-color-24',
          callback: () => {
            layerBox.value.close()
            // ----
            const wzModule = proxy.$Store.get('wz_module')
            const WXConfig = proxy.$Store.get('WXConfig')
            const forClick = proxy.$Store.get('forClick')
            proxy.$Store.clear(() => {
              proxy.$Store.set('wz_module', wzModule)
              proxy.$Store.set('WXConfig', WXConfig)
              proxy.$Store.set('forClick', forClick)
              // ----
              proxy.$Myfun.openPage(proxy, 'push', 'UserLogin', { toName: 'UcenTer' })
            })
          }
        }]
      }
      proxy.$Myfun.isIndepth(layerBox.value, 'open', options)
    }
  }]
}])

/* ---- */
const layerBox = ref()

/* ---- */
const setPublicInfo = async (item, callback) => {
  await proxy.$http[item.type](item.url, item.data).then((res) => {
    if (res) callback && callback(res)
  }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
}
// ----
const getCurrencyInfo = async () => {
  // --是否婚介交友报名--
  await proxy.$http.get('marriage-frm/index', {
    params: {
      'MarriageFrmSearch[dealer_id]': logUser.id,
      // ----
      sort: 'SORT_DESC',
      // ----
      page: 1,
      PageSize: 1
    }
  }).then((res) => {
    if (Number(res.items.length) > 0) user.marryRegId = res.items[0].id
  }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
}
// ----
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
  }
}

/* ---- */
onBeforeMount(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <div class="USS-check" v-if="son.isBtn"><input type="checkbox" class="a-switch" v-model="son.isOpen"/><label></label></div>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
    <layer-view ref="layerBox"/>
  </div>
</template>
